//
//  LPFComposeToolBar.swift
//  WeiBo
//
//  Created by mac on 16/4/23.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

enum LPFComposeToolBarType: Int{
    // 图片
    case Picture = 1001
    // @
    case Mention = 1002
    // #
    case Trend = 1003
    // 表情
    case Emoticon = 1004
    // +
    case Add = 1005
}

class LPFComposeToolBar: UIView {

    
    // emoticonButton
    var emoticonButton: UIButton?
    
    var composeToolBarColsure: ((type: LPFComposeToolBarType)->())?
    
    
    // MARK: - 判断当前是否是表情键盘
    var isEmoticon: Bool = false{
        didSet{
            if isEmoticon{
                //显示键盘图片
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }else{
                //显示表情图片
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 设置视图
    private func setupUI(){
        self.backgroundColor = RandomColor()
        
        // 添加控件
        let pictureButton = addChildButton("compose_toolbar_picture", type: .Picture)
        let mentionButton = addChildButton("compose_mentionbutton_background", type: .Mention)
        let trendButton = addChildButton("compose_trendbutton_background", type: .Trend)
        emoticonButton = addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        let addButton = addChildButton("compose_add_background", type: .Add)
        
        // 添加约束
        pictureButton.snp_makeConstraints { (make) -> Void in
            make.leading.bottom.top.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(trendButton)
            make.leading.equalTo(pictureButton.snp_trailing)
        }
        
        trendButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(emoticonButton!)
            make.leading.equalTo(mentionButton.snp_trailing)
        }
        
        emoticonButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(addButton)
            make.leading.equalTo(trendButton.snp_trailing)
        }
        
        addButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
    }
    
    // MARK: - 添加按钮的公共方法
    func addChildButton(imgName: String, type: LPFComposeToolBarType) -> UIButton{
        let button = UIButton()
        // 把枚举值转成int
        button.tag = type.rawValue
        // 添加点击事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        addSubview(button)
        return button
    }
    
    // MARK: -  监听事件
    @objc private func buttonClick(button: UIButton){
        // CZComposeToolBarType(rawValue: button.tag) 把int类型转成枚举值
        composeToolBarColsure?(type: LPFComposeToolBarType(rawValue: button.tag)!)
    }
}
